/*
 * Copyright 2012 Alain Pannetrat (L1L1@gmx.com)
 * 
 * This file is part of cookie-miner.
 * 
 * cookie-miner is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * cookie-miner is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with cookie-miner. If not, see http://www.gnu.org/licenses/.
 *
 * @version: $Id$
 *
 */
#ifndef _GFX_WIDGETS_GTK_H_
#define _GFX_WIDGETS_GTK_H_

#include "common.h"
#include <SDL.h>
#include <vector>

namespace gfx {
	typedef SDL_Event Event;
	class Rectangle {
		public:
			Rectangle(int sleft=0, int stop=0, int sright=0, int sbottom=0) : 
				_left(sleft), 
				_top(stop), 
				_right(sright), 
				_bottom(sbottom) {}
			void offset(int dx, int dy) { 
				_left  +=dx; 
				_top   +=dy; 
				_right +=dx;
				_bottom+=dy; }
			void set(int sleft, int stop, int sright, int sbottom) { 
				_left = sleft; 
				_top = stop; 
				_right = sright;
				_bottom = sbottom; }

			bool is_inside(int x, int y) const {
				return (x>=_left && x<=_right && y>=_right && y<=_right); }

			int left() const { return _left; }
			void left(int sleft) { _left = sleft; }
			int top() const { return _top; }
			void top(int stop) { _top = stop; }
			int right() const { return _right; }
			void right(int sright) { _right = sright; }
			int bottom() const { return _bottom; }
			void bottom(int sbottom) { _bottom = sbottom; }

			int width() const { return _right-_left+1; }
			int height() const { return _bottom-_top+1; }
		private:
			int _left;
			int _top;
			int _right;
			int _bottom;
	};
	class Style {
		public:
			Rectangle pos;
			Rectangle margin;
			Rectangle padding;

			Style() : pos(-1,-1,-1,-1), margin(0,0,0,0), padding(0,0,0,0) { }

			void margin_all(int m) { margin.set(m,m,m,m); }		 
	
			void padding_all(int p) { padding.set(p,p,p,p); }		 
	};
	extern Style default_style; 

	class Widget {
		public:
			Widget(Rectangle pos = Rectangle(0,0,0,0)) : _style(default_style), _pos(pos) {}	
			Widget(int w, int h) : _style(default_style), _pos(0,0,w-1,h-1) {}	

			const Style& style() const { return _style; }
			void style(const Style& style) { _style = style; }
			
			virtual const Rectangle& position() const { return _pos; }
			virtual void position(const Rectangle& pos) { _pos = pos; }				
			virtual bool init();
			virtual bool draw();
			virtual bool on_release(const Event& ev) { return true; }
			virtual bool on_click(const Event& ev) { return true; }
			virtual bool on_double_click(const Event& ev) {	return true; }
			virtual bool on_mouse_move(const Event& ev) { return true; } 
			virtual bool on_key_press(const Event& ev) { return true; }
			virtual bool resize() { return true; }
		protected:
			
			Style _style;
			Rectangle _pos;
		private:

			DISALLOW_COPY_AND_ASSIGN(Widget);	
	};

	class Container : public Widget {
		public:
			Container(Rectangle pos = Rectangle(0,0,0,0)) : Widget(pos) {}
			Container(int w, int h) : Widget(w,h) {}
			
			virtual bool init();
			virtual bool draw();	
			virtual bool on_release(const Event& ev);
			virtual bool on_click(const Event& ev);
			virtual bool on_double_click(const Event& ev);
			virtual bool on_mouse_move(const Event& ev);
			virtual bool on_key_press(const Event& ev);
			virtual bool resize();
		protected:	
		private:
			std::vector<Widget*> _children;
			
			DISALLOW_COPY_AND_ASSIGN(Container);	
	};

}

#endif

